//
//  BHCircleDetailBannerHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/12.
//

import UIKit
protocol BHCircleDetailBannerHeaderViewDelegate {
    func circleDetailPlayVideo()
}

class BHCircleDetailBannerHeaderView: UIView {
    
    var delegate: BHCircleDetailBannerHeaderViewDelegate?
    

    @IBOutlet weak var place_imageView: UIImageView!
        
    @IBOutlet weak var videoPlayBtn: UIButton!
    
    lazy var bannerView: BHCycleView = {
        let view = BHCycleView.init()
        view.backgroundColor = .color_f8f8f8
        view.collectionView.backgroundColor = .color_f8f8f8
        view.scrollDirection = .horizontal
        view.timerValue = 0
        view.leadingAndTralingSpace = 0
        view.lineSpace = 0
        view.dataSource = self
        view.delegate = self
        return view
    }()
    var imageUrls: [String] = [] {
        didSet {
            self.bannerView.sourceArray = imageUrls
        }
    }
    
    var videoUrl: String = "" {
        didSet {
            self.bannerView.isHidden = true
            self.place_imageView.isHidden = false
            self.videoPlayBtn.isHidden = false
            if videoUrl.isEmpty == false {
                self.place_imageView.image = BHCommonTool.thumbImage(url: videoUrl)
            }
        }
    }
    
    
    
    static func loadFromXib() -> BHCircleDetailBannerHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHCircleDetailBannerHeaderView", owner: nil, options: nil)
        let view: BHCircleDetailBannerHeaderView = viewArray![0] as! BHCircleDetailBannerHeaderView
        return view
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.addSubview(self.bannerView)
        self.bannerView.mas_makeConstraints { (make) in
            make?.left.right()?.bottom()?.top()?.equalTo()(self.place_imageView)
        }
        
        self.videoPlayBtn.addBtnAction(action: {[weak self] (_) in
            self?.delegate?.circleDetailPlayVideo()
        }, for: .touchUpInside)
    }
       
}
extension BHCircleDetailBannerHeaderView: BHCycleViewDataSource,BHCycleViewDelegate {
    func cellForRow(_ indexPath: IndexPath, _ collectionView: UICollectionView,_ cycleView: UIView) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCommonBannerImageCell", for: indexPath) as! BHCommonBannerImageCell
        let url = self.imageUrls[indexPath.item%self.imageUrls.count]
        cell.bannerImageUrl = url
        cell.backgroundColor = .clear
        return cell
    }
    func sizeForItem(_ indexPath: IndexPath,_ cycleView: UIView) -> CGSize {
        return CGSize(width: UIScreen.width, height: UIScreen.width)
    }
    
    func numberOfItem(_ cycleView: UIView) -> Int {
        return self.imageUrls.count
    }
    func didSelectItem(_ index: Int,_ cycleView: UIView) {

    }
}
